﻿namespace HutongGames.PlayMakerEditor
{
    using HutongGames.PlayMaker;
    using System;
    using System.Collections.Generic;
    using UnityEditor;
    using UnityEngine;

    public class ActionReportWindow : EditorWindow
    {
        private FsmStateAction currentAction;
        private PlayMakerFSM currentFSM;
        private FsmState currentState;
        private Vector2 scrollPosition;
        private readonly string[] sortOptions = new string[] { "Sort By FSM", "Sort By Action" };

        private static void DoReportLine(string prefix, ActionReport report)
        {
            if (report.isError)
            {
                GUILayout.Box(prefix + report.logText, FsmEditorStyles.ErrorBox, new GUILayoutOption[0]);
            }
            else
            {
                GUILayout.Label(prefix + report.logText, new GUILayoutOption[0]);
            }
        }

        private void DoSortedByAction()
        {
            List<System.Type> list = new List<System.Type>();
            foreach (ActionReport report in ActionReport.ActionReportList)
            {
                System.Type item = report.action.GetType();
                if (!list.Contains(item))
                {
                    list.Add(item);
                }
            }
            this.currentAction = null;
            foreach (System.Type type2 in list)
            {
                FsmEditorGUILayout.Divider(new GUILayoutOption[0]);
                GUILayout.Label(FsmEditorUtility.GetActionLabel(type2), EditorStyles.boldLabel, new GUILayoutOption[0]);
                this.currentFSM = null;
                this.currentState = null;
                List<FsmState> list2 = new List<FsmState>();
                List<ActionReport> list3 = new List<ActionReport>();
                List<string> list4 = new List<string>();
                FsmEditorGUILayout.LightDivider(new GUILayoutOption[0]);
                GUILayout.Label("\tAction Changes:", EditorStyles.boldLabel, new GUILayoutOption[0]);
                foreach (ActionReport report2 in ActionReport.ActionReportList)
                {
                    if (report2.action.GetType() == type2)
                    {
                        if (!list2.Contains(report2.state))
                        {
                            list3.Add(report2);
                            list2.Add(report2.state);
                        }
                        if (!list4.Contains(report2.logText))
                        {
                            DoReportLine("\t", report2);
                            list4.Add(report2.logText);
                        }
                    }
                }
                FsmEditorGUILayout.LightDivider(new GUILayoutOption[0]);
                GUILayout.Label("\tAffected States:", EditorStyles.boldLabel, new GUILayoutOption[0]);
                foreach (ActionReport report3 in list3)
                {
                    if ((report3.state != null) && (report3.fsm != null))
                    {
                        if (GUILayout.Button("\t" + FsmEditorUtility.GetFullStateLabel(report3.state), EditorStyles.label, new GUILayoutOption[0]))
                        {
                            SelectReport(report3);
                            break;
                        }
                        EditorGUIUtility.AddCursorRect(GUILayoutUtility.GetLastRect(), MouseCursor.Link);
                    }
                }
            }
        }

        private void DoSortedByFSM()
        {
            this.currentFSM = null;
            this.currentState = null;
            this.currentAction = null;
            foreach (ActionReport report in ActionReport.ActionReportList)
            {
                if (((report.fsm != null) && (report.state != null)) && (report.action != null))
                {
                    if (report.fsm != this.currentFSM)
                    {
                        this.currentFSM = report.fsm;
                        FsmEditorGUILayout.Divider(new GUILayoutOption[0]);
                        if (GUILayout.Button(FsmEditorUtility.GetFullFsmLabel(report.fsm.Fsm), EditorStyles.label, new GUILayoutOption[0]))
                        {
                            SelectReport(report);
                            break;
                        }
                        EditorGUIUtility.AddCursorRect(GUILayoutUtility.GetLastRect(), MouseCursor.Link);
                    }
                    if (report.state != this.currentState)
                    {
                        this.currentState = report.state;
                        FsmEditorGUILayout.LightDivider(new GUILayoutOption[0]);
                        if (GUILayout.Button("\t" + report.state.Name, EditorStyles.label, new GUILayoutOption[0]))
                        {
                            SelectReport(report);
                            break;
                        }
                        EditorGUIUtility.AddCursorRect(GUILayoutUtility.GetLastRect(), MouseCursor.Link);
                    }
                    if (report.action != this.currentAction)
                    {
                        FsmEditorGUILayout.LightDivider(new GUILayoutOption[0]);
                        this.currentAction = report.action;
                        if (GUILayout.Button("\t\t" + FsmEditorUtility.GetActionLabel(report.action), EditorStyles.label, new GUILayoutOption[0]))
                        {
                            SelectReport(report);
                            break;
                        }
                        EditorGUIUtility.AddCursorRect(GUILayoutUtility.GetLastRect(), MouseCursor.Link);
                    }
                    DoReportLine("\t\t\t", report);
                }
            }
        }

        private void DoToolbar()
        {
            GUILayout.BeginHorizontal(EditorStyles.toolbar, new GUILayoutOption[0]);
            FsmEditorSettings.ConsoleActionReportSortOptionIndex = EditorGUILayout.Popup(FsmEditorSettings.ConsoleActionReportSortOptionIndex, this.sortOptions, EditorStyles.toolbarPopup, new GUILayoutOption[0]);
            if (GUI.changed)
            {
                FsmEditorSettings.SaveSettings();
            }
            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();
        }

        private void OnEnable()
        {
            base.title = "Console";
            base.autoRepaintOnSceneChange = true;
        }

        private void OnGUI()
        {
            if (FsmEditor.Instance == null)
            {
                base.Close();
            }
            else
            {
                if (EditorApplication.isCompiling)
                {
                    GUI.enabled = false;
                }
                if (!FsmEditorStyles.IsInitialized())
                {
                    FsmEditorStyles.Init();
                }
                this.DoToolbar();
                this.scrollPosition = GUILayout.BeginScrollView(this.scrollPosition, new GUILayoutOption[0]);
                GUILayout.Box("This log helps you track down potential problems caused by Actions changing since the project was saved. Click the FSM/State/Action to select it in the main editor window and quickly jump to the change.", FsmEditorStyles.InfoBox, new GUILayoutOption[0]);
                switch (FsmEditorSettings.ConsoleActionReportSortOptionIndex)
                {
                    case 0:
                        this.DoSortedByFSM();
                        break;

                    case 1:
                        this.DoSortedByAction();
                        break;
                }
                if (ActionReport.ActionReportList.Count == 0)
                {
                    GUILayout.Label("No warnings or errors...", new GUILayoutOption[0]);
                }
                GUILayout.EndScrollView();
            }
        }

        private static void SelectReport(ActionReport report)
        {
            FsmEditor.SelectFsm(report.fsm.Fsm);
            FsmEditor.SelectState(report.state, true);
            FsmEditor.SelectAction(report.state, report.actionIndex, true);
        }
    }
}

